
import * as API_WFREPORT from '../../../apis/efficiencyReport';
import { WeaRadioGroup,WeaBrowser,WeaSelect} from 'ecCom';
import isEqual from 'lodash/isEqual';
import * as mobx from 'mobx';
import isEmpty from "lodash/isEmpty";
/**
 * 高级查询
 * props
 * advanceSearchPara ： 高级查询数据
 */
export default class Main extends React.Component{
	constructor(props) {
		super(props);
		this.state = {
			isShowAllType:false,
			options:[],
		}
	}
	
	shouldComponentUpdate(nextProps,nextState) {
		const _c = !isEqual(mobx.toJS(this.props.advanceSearchData), mobx.toJS(nextProps.advanceSearchData));
        return _c||this.props.iswf !== nextProps.iswf
        		 ||this.state.isShowAllType !== nextState.isShowAllType
        		 ||!isEqual(this.state.options,nextState.options);
    }
	
	componentWillReceiveProps(nextProps){
		const {advanceSearchPara,iswf = false} = this.props;
		const {workflowid = -1} = advanceSearchPara;
		const {options =[]} = this.state;
		if(iswf && options.length === 0 &&  workflowid > -1) {
			this.getWfVersions(workflowid);
		}
		
		if(!iswf && options.length > 0 ){
			this.setState({options:[]});
		}
	}
	
	render(){
		const config = this.getAdvanceSearchConfig();
		return (
			<div className="advance-search-container">
                {!isEmpty(config) && <WeaRadioGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaRadioGroup@sybudj`} config={config} onChange={this.doConditonChange.bind(this)} beforeInit={this.doBeforeInit.bind(this)}/>}
			</div>
		)
	}
	
	//初始化配置数据
	getAdvanceSearchConfig(){
		const {advanceSearchData = {},advanceSearchPara = {} ,iswf = false } = this.props;
		const {wftypeinfo = {},wfinfo = {}} = advanceSearchData;
		//更新label
		const {config =[]} = advanceSearchData;
		const ___config  = mobx.toJS(config);
        if(isEmpty(___config)) return ___config;
		if(!iswf){
			const __wftypeinfo = mobx.toJS(wftypeinfo);
			const {options = [],domkey} = __wftypeinfo;
			const {wftypeid = '-1'} = advanceSearchPara;
			const {isShowAllType} = this.state;
			const wrapWfTypeClassName = isShowAllType ? '' :"show-line";
			const com = <div className={`flow-time-report-wftype-conditon ${wrapWfTypeClassName}`}>
							<WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@94uv0p`}
						      detailtype={4}
						      options={options}
						      value={wftypeid}
						      onChange={(value) => this.onChange(value)}
						    />
						    <div className="wftype-show-all" onClick={()=>this.setState({isShowAllType:!isShowAllType})}>
								<span className={isShowAllType ? 'icon-coms-up' : 'icon-coms-down'}/>
							</div>
					    </div>;

			__wftypeinfo.options = [];
			__wftypeinfo.com = com;
			___config.push(__wftypeinfo);
		}else{
			const __wfinfo = mobx.toJS(wfinfo);
			const {workflowid = -1,workflowname=''} = advanceSearchPara;
			const replaceDatas = [{id:workflowid,name:workflowname}];
			const {wfbrowprop = {}} = __wfinfo;
			wfbrowprop.replaceDatas = replaceDatas;
			const {options=[]} = this.state;
			const com  = <div className="flow-time-report-wfinfo-conditon">
							<WeaBrowser ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@l8bqpi`} {...wfbrowprop} onChange={this.wfBrowserChangeEvent.bind(this)}/>
							{options.length > 0 && <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@uqzf0m`} options={options} value={options.length > 0 ? workflowid:''} onChange={this.wfVersionChangeEvent.bind(this)}/>}
						</div>;
			__wfinfo.com = com;
			___config.push(__wfinfo);
		}
		return ___config;
	}
	
	doConditonChange(data = {}){
		const {updateAdvanceSearchPara} = this.props;
		updateAdvanceSearchPara && updateAdvanceSearchPara(data);
	}
	
	onChange(value){
		const {updateAdvanceSearchPara} = this.props;
		updateAdvanceSearchPara({wftypeid:value});
	}
	
	wfBrowserChangeEvent(ids,names,datas){
		const {updateAdvanceSearchPara} = this.props;
		updateAdvanceSearchPara && updateAdvanceSearchPara({workflowid:ids,workflowname:names});
		this.getWfVersions(ids);
	}
	
	wfVersionChangeEvent(workflowid){
		const {updateAdvanceSearchPara} = this.props;
		updateAdvanceSearchPara && updateAdvanceSearchPara({workflowid});
	}
	
	getWfVersions(workflowid){
		this.setState({options:[]});
		API_WFREPORT.getFlowTimeAnalyseWfVersions({workflowid}).then((data = {})=>{
			const {wfVersions = []} = data;
			this.setState({options:wfVersions});
		});
	}
	
	doBeforeInit(config,lastValueMap = {},callback){
		callback(lastValueMap);
	}
}